import time

from selenium.webdriver.common.by import By

from page_object.pages.BasePage import BasePage
from page_object.pages.SearchResultPage import SearchResultPage


class SearchPage(BasePage):
    # 使用CLASS_NAME定位到搜索框
    _edit_locator = (By.CLASS_NAME, "android.widget.EditText")
    select_result = (By.CLASS_NAME, "android.widget.RelativeLayout")

    def seach(self, key, result):
        """
          定义搜索方法
          :param key:股票名称
          :return: 返回自己页面
          """
        self.find(self._edit_locator).send_keys(key)
        time.sleep(2)
        click = (By.XPATH, "//*[contains(@resource-id, 'name') and (@text = '%s')]" % result)
        # self.find_by_xpath(result).click()
        self.find(click).click()
        return self
    def searchHistory(self, key):
        """
          点击搜索历史
          :param key: 名称
          :return:
          """
        self.find_by_xpath(key)
        self.find_by_xpath(key).click()
        return self

    def addToSelected(self, key):
        """
        添加股票
        :param key: 股票代码
        :return:
        """
        follow_button = (By.XPATH,
                         "//*[contains(@resource-id, 'stockCode') and contains(@text, '%s')]/../../.." % key +
                         "//*[contains(@resource-id, 'follow_btn')]")
        self.find(follow_button).click()
        return self
    def removeToSelected(self, key):
        """
        取消添加股票
        :param key: 股票代码
        :return:
        """
        followed_button = (By.XPATH,
                         "//*[contains(@resource-id, 'stockCode') and contains(@text, '%s')]/../../.." % key +
                         "//*[contains(@resource-id, 'followed_btn')]")
        self.find(followed_button).click()
        return self

    def isInSelected(self, key):
        """
        判断是否关注某一支股票
        :param key: 股票代码
        :return: 未关注返回：False，关注返回：True
        """
        follow_button = (By.XPATH,
                         "//*[contains(@resource-id, 'stockCode') and contains(@text, '%s')]/../../.." % key +
                         "//*[contains(@resource-id, 'follow')]")
        id = self.find(follow_button).get_attribute("resourceId")
        print(id)
        return 'followed_btn' in id

    def searchByUser(self, key):
        """
        搜索用户方法
        :param key:搜索用户名
        :return:
        """
        self.find(self._edit_locator).send_keys(key)
        searchResult = (By.ID, "name")
        time.sleep(1)
        self.find(searchResult).click()
        self.find_by_xpath("用户").click()
        return self
    def isFollowed(self, key):
        """
        判断是否关注
        :param key:用户名
        :return: 未关注返回False，关注返回Ture
        """
        follow_button = (By.XPATH,
                         "//*[contains(@resource-id, 'user_name') and (@text = '%s')]/../.." % key +
                         "//*[contains(@resource-id, 'btn')]")
        id = self.find(follow_button).get_attribute("resourceId")
        print(id)
        return id == 'com.xueqiu.android:id/followed_btn'

    def cancel(self):
        self.find_by_xpath('取消').click()

